package com.xudow.app.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.Activities;
import com.xudow.app.R;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 活动消息ListView Adapter
 *
 * @author lukecheng
 */
public class ActivityMessageAdapter extends RecyclerView.Adapter {

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private List<Activities> activities;

    public ActivityMessageAdapter(List<Activities> activities) {
        this.activities = activities;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity_message_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MessageViewHolder viewHolder = (MessageViewHolder) holder;

        Activities ac = activities.get(position);
        viewHolder.title.setText(ac.getTitle());
        viewHolder.time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(ac.getActivityStartDate()));

        // TODO:活动需要简介
        // viewHolder.enrollCount.setText(ac.getQuota());

        //TODO:活动图片
    }

    @Override
    public int getItemCount() {
        return activities.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private LinearLayout root;
        public ImageView image;
        public TextView title;
        public TextView time;
        public TextView enrollCount;

        public MessageViewHolder(View itemView) {
            super(itemView);

            root = (LinearLayout) itemView.findViewById(R.id.root);
            root.setOnClickListener(this);
            root.setOnLongClickListener(this);

            image = (ImageView) itemView.findViewById(R.id.activity_img);
            title = (TextView) itemView.findViewById(R.id.title);
            enrollCount = (TextView) itemView.findViewById(R.id.enroll_count);
            time = (TextView) itemView.findViewById(R.id.time);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
